<!DOCTYPE html>
<html lang=zh>
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="utf-8">
  
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="renderer" content="webkit">
  <meta http-equiv="Cache-Control" content="no-transform">
  <meta http-equiv="Cache-Control" content="no-siteapp">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="format-detection" content="telephone=no,email=no,adress=no">
  <!-- Color theme for statusbar -->
  <meta name="theme-color" content="#000000">
  <!-- 强制页面在当前窗口以独立页面显示,防止别人在框架里调用页面 -->
  <meta http-equiv="window-target" content="_top">
  
  
  <title>Shell脚本常用命令 | 王竹兴 | Blob</title>
  <meta name="description" content="概述 ​	Shell 是一个用 C 语言编写的程序，它是用户使用 Linux 的桥梁。Shell 既是一种命令语言，又是一种程序设计语言。 ​	Shell 脚本（shell script），是一种为 shell 编写的脚本程序。  set 命令  set -e表示一旦脚本中有命令的返回值为非0，则脚本立即退出，后续命令不再执行; set -o pipefail表示在管道连接的命令序列中，只要有任何">
<meta name="keywords" content="scripts">
<meta property="og:type" content="article">
<meta property="og:title" content="Shell脚本常用命令">
<meta property="og:url" content="http://zhulao.gitee.io/blog/2019/02/26/Shell脚本常用命令/index.html">
<meta property="og:site_name" content="王竹兴 | Blog">
<meta property="og:description" content="概述 ​	Shell 是一个用 C 语言编写的程序，它是用户使用 Linux 的桥梁。Shell 既是一种命令语言，又是一种程序设计语言。 ​	Shell 脚本（shell script），是一种为 shell 编写的脚本程序。  set 命令  set -e表示一旦脚本中有命令的返回值为非0，则脚本立即退出，后续命令不再执行; set -o pipefail表示在管道连接的命令序列中，只要有任何">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-04-27T14:15:40.329Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Shell脚本常用命令">
<meta name="twitter:description" content="概述 ​	Shell 是一个用 C 语言编写的程序，它是用户使用 Linux 的桥梁。Shell 既是一种命令语言，又是一种程序设计语言。 ​	Shell 脚本（shell script），是一种为 shell 编写的脚本程序。  set 命令  set -e表示一旦脚本中有命令的返回值为非0，则脚本立即退出，后续命令不再执行; set -o pipefail表示在管道连接的命令序列中，只要有任何">
  <!-- Canonical links -->
  <link rel="canonical" href="http://zhulao.gitee.io/blog/2019/02/26/Shell脚本常用命令/index.html">
  
    <link rel="alternate" href="/atom.xml" title="王竹兴 | Blog" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png" type="image/x-icon">
  
  <link rel="stylesheet" href="/blog/css/style.css">
  
  
  
  
</head>


<body class="main-center" itemscope itemtype="http://schema.org/WebPage">
  <header class="header" itemscope itemtype="http://schema.org/WPHeader">
  <div class="slimContent">
    <div class="navbar-header">
      
      
      <div class="profile-block text-center">
        <a id="avatar" href="https://github.com/wzx3" target="_blank">
          <img class="img-circle img-rotate" src="/blog/images/avatar.jpeg" width="200" height="200">
        </a>
        <h2 id="name" class="hidden-xs hidden-sm">王竹兴</h2>
        <h3 id="title" class="hidden-xs hidden-sm hidden-md"></h3>
        <small id="location" class="text-muted hidden-xs hidden-sm"><i class="icon icon-map-marker"></i> XiaMen, China</small>
      </div>
      
      <div class="search" id="search-form-wrap">

    <form class="search-form sidebar-form">
        <div class="input-group">
            <input type="text" class="search-form-input form-control" placeholder="搜索">
            <span class="input-group-btn">
                <button type="submit" class="search-form-submit btn btn-flat" onclick="return false;"><i class="icon icon-search"></i></button>
            </span>
        </div>
    </form>
    <div class="ins-search">
  <div class="ins-search-mask"></div>
  <div class="ins-search-container">
    <div class="ins-input-wrapper">
      <input type="text" class="ins-search-input" placeholder="想要查找什么..." x-webkit-speech>
      <button type="button" class="close ins-close ins-selectable" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
    </div>
    <div class="ins-section-wrapper">
      <div class="ins-section-container"></div>
    </div>
  </div>
</div>


</div>
      <button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target="#main-navbar" aria-controls="main-navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>
    <nav id="main-navbar" class="collapse navbar-collapse" itemscope itemtype="http://schema.org/SiteNavigationElement" role="navigation">
      <ul class="nav navbar-nav main-nav menu-highlight">
        
        
        <li class="menu-item menu-item-home">
          <a href="/blog/.">
            
            <i class="icon icon-home-fill"></i>
            
            <span class="menu-title">首页</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives">
            
            <i class="icon icon-archives-fill"></i>
            
            <span class="menu-title">归档</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories">
            
            <i class="icon icon-folder"></i>
            
            <span class="menu-title">分类</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags">
            
            <i class="icon icon-tags"></i>
            
            <span class="menu-title">标签</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-repository">
          <a href="/blog/repository">
            
            <i class="icon icon-project"></i>
            
            <span class="menu-title">项目</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-about">
          <a href="/blog/about">
            
            <i class="icon icon-cup-fill"></i>
            
            <span class="menu-title">关于</span>
          </a>
        </li>
        
      </ul>
      
	
    <ul class="social-links">
    	
        <li><a href="https://github.com/wzx3" target="_blank" title="Github" data-toggle="tooltip" data-placement="top"><i class="icon icon-github"></i></a></li>
        
        <li><a href="/blog/atom.xml" target="_blank" title="Rss" data-toggle="tooltip" data-placement="top"><i class="icon icon-rss"></i></a></li>
        
    </ul>

    </nav>
  </div>
</header>

  
    <aside class="sidebar" itemscope itemtype="http://schema.org/WPSideBar">
  <div class="slimContent">
    
      <div class="widget">
    <h3 class="widget-title">公告</h3>
    <div class="widget-body">
        <div id="board">
            <div class="content">
                <p>人生最快意的事情之一, 就是你比别人牛逼, 然后静静的看别人在你面前装逼。</p>
            </div>
        </div>
    </div>
</div>

    
      
  <div class="widget">
    <h3 class="widget-title">分类</h3>
    <div class="widget-body">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/blog/categories/便签/">便签</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/教程/">教程</a><span class="category-list-count">22</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/游记/">游记</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/随笔/">随笔</a><span class="category-list-count">6</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">标签云</h3>
    <div class="widget-body tagcloud">
      <a href="/blog/tags/DL/" style="font-size: 13.25px;">DL</a> <a href="/blog/tags/Debug/" style="font-size: 13.5px;">Debug</a> <a href="/blog/tags/Doc/" style="font-size: 13px;">Doc</a> <a href="/blog/tags/Hexo/" style="font-size: 13.25px;">Hexo</a> <a href="/blog/tags/Links/" style="font-size: 13px;">Links</a> <a href="/blog/tags/Linux/" style="font-size: 13.25px;">Linux</a> <a href="/blog/tags/Mac/" style="font-size: 13px;">Mac</a> <a href="/blog/tags/bazel/" style="font-size: 13px;">bazel</a> <a href="/blog/tags/c-c/" style="font-size: 13.75px;">c/c++</a> <a href="/blog/tags/git/" style="font-size: 13px;">git</a> <a href="/blog/tags/rst/" style="font-size: 13px;">rst</a> <a href="/blog/tags/scripts/" style="font-size: 13.75px;">scripts</a> <a href="/blog/tags/ssh/" style="font-size: 13px;">ssh</a> <a href="/blog/tags/国学经典/" style="font-size: 13px;">国学经典</a> <a href="/blog/tags/持续集成/" style="font-size: 13.25px;">持续集成</a> <a href="/blog/tags/科幻小说/" style="font-size: 14px;">科幻小说</a>
    </div>
  </div>

    
      
  <div class="widget">
    <h3 class="widget-title">归档</h3>
    <div class="widget-body">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2019/05/">五月 2019</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2019/04/">四月 2019</a><span class="archive-list-count">10</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2019/03/">三月 2019</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2019/02/">二月 2019</a><span class="archive-list-count">10</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget-body">
      <ul class="recent-post-list list-unstyled no-thumbnail">
        
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/blog/categories/随笔/">随笔</a>
              </p>
              <p class="item-title">
                <a href="/blog/2019/05/26/读论语/" class="title">读论语</a>
              </p>
              <p class="item-date">
                <time datetime="2019-05-26T01:45:07.000Z" itemprop="datePublished">2019-05-26</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/blog/categories/随笔/">随笔</a>
              </p>
              <p class="item-title">
                <a href="/blog/2019/05/20/读变型战争/" class="title">读变型战争</a>
              </p>
              <p class="item-date">
                <time datetime="2019-05-20T15:51:57.000Z" itemprop="datePublished">2019-05-20</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/blog/categories/随笔/">随笔</a>
              </p>
              <p class="item-title">
                <a href="/blog/2019/05/20/读星际远征/" class="title">读黑客横行</a>
              </p>
              <p class="item-date">
                <time datetime="2019-05-20T15:40:09.000Z" itemprop="datePublished">2019-05-20</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/blog/categories/教程/">教程</a>
              </p>
              <p class="item-title">
                <a href="/blog/2019/05/15/代码格式化工具-clang-format/" class="title">代码格式化工具:clang-format</a>
              </p>
              <p class="item-date">
                <time datetime="2019-05-15T14:39:51.000Z" itemprop="datePublished">2019-05-15</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/blog/categories/教程/">教程</a>
              </p>
              <p class="item-title">
                <a href="/blog/2019/05/12/Linux-疑难杂症/" class="title">Linux 疑难杂症</a>
              </p>
              <p class="item-date">
                <time datetime="2019-05-12T02:50:29.000Z" itemprop="datePublished">2019-05-12</time>
              </p>
            </div>
          </li>
          
      </ul>
    </div>
  </div>
  

    
  </div>
</aside>

  
  
<aside class="sidebar sidebar-toc collapse" id="collapseToc" itemscope itemtype="http://schema.org/WPSideBar">
  <div class="slimContent">
    <nav id="toc" class="article-toc">
      <h3 class="toc-title">文章目录</h3>
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#set-命令"><span class="toc-number">1.</span> <span class="toc-text"> set 命令</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#常用特殊变量"><span class="toc-number">2.</span> <span class="toc-text"> 常用特殊变量</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#数值判断"><span class="toc-number">3.</span> <span class="toc-text"> 数值判断</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#逻辑判断"><span class="toc-number">4.</span> <span class="toc-text"> 逻辑判断</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#字符串判断"><span class="toc-number">5.</span> <span class="toc-text"> 字符串判断</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#去除字符串的双引号"><span class="toc-number">6.</span> <span class="toc-text"> 去除字符串的双引号</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#文件目录判断"><span class="toc-number">7.</span> <span class="toc-text"> 文件/目录判断</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#提取文件名和目录名"><span class="toc-number">8.</span> <span class="toc-text"> 提取文件名和目录名</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#从路径中获取文件名"><span class="toc-number">8.1.</span> <span class="toc-text"> 从路径中获取文件名</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#从路径中获取文件后缀名"><span class="toc-number">8.2.</span> <span class="toc-text"> 从路径中获取文件后缀名</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#从路径中获取文件所在目录路径"><span class="toc-number">9.</span> <span class="toc-text"> 从路径中获取文件所在目录路径</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#变量替换"><span class="toc-number">10.</span> <span class="toc-text"> 变量替换</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#数组遍历"><span class="toc-number">11.</span> <span class="toc-text"> 数组遍历</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#for循环"><span class="toc-number">11.1.</span> <span class="toc-text"> for循环</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#带数组下标"><span class="toc-number">11.1.1.</span> <span class="toc-text"> 带数组下标</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#不带数组下标"><span class="toc-number">11.1.2.</span> <span class="toc-text"> 不带数组下标</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#while循环"><span class="toc-number">11.2.</span> <span class="toc-text"> While循环</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#解析脚本传参"><span class="toc-number">11.3.</span> <span class="toc-text"> 解析脚本传参</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#格式化输出"><span class="toc-number">11.4.</span> <span class="toc-text"> 格式化输出</span></a></li></ol></li></ol>
    </nav>
  </div>
</aside>

<main class="main" role="main">
  <div class="content">
  <article id="post-Shell脚本常用命令" class="article article-type-post" itemscope itemtype="http://schema.org/BlogPosting">
    
    <div class="article-header">
      
        
  
    <h1 class="article-title" itemprop="name">
      Shell脚本常用命令
    </h1>
  

      
      <div class="article-meta">
        <span class="article-date">
    <i class="icon icon-calendar-check"></i>
	<a href="/blog/2019/02/26/Shell脚本常用命令/" class="article-date">
	  <time datetime="2019-02-26T13:29:06.000Z" itemprop="datePublished">2019-02-26</time>
	</a>
</span>
        
  <span class="article-category">
    <i class="icon icon-folder"></i>
    <a class="article-category-link" href="/blog/categories/教程/">教程</a>
  </span>

        
  <span class="article-tag">
    <i class="icon icon-tags"></i>
	<a class="article-tag-link" href="/blog/tags/scripts/">scripts</a>
  </span>


        
	<span class="article-read hidden-xs">
	    <i class="icon icon-eye-fill" aria-hidden="true"></i>
	    <span id="busuanzi_container_page_pv">
			<span id="busuanzi_value_page_pv">0</span>
		</span>
	</span>


        <span class="post-comment"><i class="icon icon-comment"></i> <a href="/blog/2019/02/26/Shell脚本常用命令/#comments" class="article-comment-link">评论</a></span>
        
	
		<span class="post-wordcount hidden-xs" itemprop="wordCount">字数统计: 1,573(字)</span>
	
	
		<span class="post-readcount hidden-xs" itemprop="timeRequired">阅读时长: 7(分)</span>
	

      </div>
    </div>
    <div class="article-entry marked-body" itemprop="articleBody">
      
        <p><strong>概述</strong></p>
<p>​	Shell 是一个用 C 语言编写的程序，它是用户使用 Linux 的桥梁。Shell 既是一种命令语言，又是一种程序设计语言。</p>
<p>​	Shell 脚本（shell script），是一种为 shell 编写的脚本程序。</p>
<h2 id="set-命令"><a class="markdownIt-Anchor" href="#set-命令"></a> set 命令</h2>
<ul>
<li><code>set -e</code>表示一旦脚本中有命令的返回值为非0，则脚本立即退出，后续命令不再执行;</li>
<li><code>set -o pipefail</code>表示在管道连接的命令序列中，只要有任何一个命令返回非0值，则整个管道返回非0值，即使最后一个命令返回0.</li>
<li><code>set -u</code>遇到不存在的变量就会报错，并停止执行。效果等同 <code>set -o nounset</code></li>
</ul>
<h2 id="常用特殊变量"><a class="markdownIt-Anchor" href="#常用特殊变量"></a> 常用特殊变量</h2>
<table>
<thead>
<tr>
<th style="text-align:center">变量</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>$0</code></td>
<td><strong>当前脚本的文件名</strong></td>
</tr>
<tr>
<td style="text-align:center"><code>$n</code></td>
<td>传递给脚本或函数的参数。<strong>n 是一个数字</strong>，表示第几个参数。例如，第一个参数是$1，第二个参数是$2</td>
</tr>
<tr>
<td style="text-align:center"><code>$#</code></td>
<td>传递给脚本或函数的<strong>参数个数</strong></td>
</tr>
<tr>
<td style="text-align:center"><code>$*</code></td>
<td>传递给脚本或函数的<strong>所有参数</strong></td>
</tr>
<tr>
<td style="text-align:center"><code>$@</code></td>
<td>传递给脚本或函数的所有参数。被双引号(” “)包含时，与 $* 稍有不同</td>
</tr>
<tr>
<td style="text-align:center"><code>$?</code></td>
<td>上个命令的退出状态，或函数的返回值。成功返回0，失败返回1</td>
</tr>
<tr>
<td style="text-align:center"><code>$$</code></td>
<td>当前Shell进程ID。对于 Shell 脚本，就是这些脚本所在的进程ID</td>
</tr>
</tbody>
</table>
<p><code>$*</code> 和 <code>$@</code> 都是将参数一个一个返回 。<br>
<code>&quot;$*&quot;</code>将所有参数当做一个整体字符串返回 , <code>&quot;$@&quot;</code>将参数一个一个返回。</p>
<h2 id="数值判断"><a class="markdownIt-Anchor" href="#数值判断"></a> 数值判断</h2>
<table>
<thead>
<tr>
<th style="text-align:center">命令</th>
<th style="text-align:center">含义</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>-eq</code></td>
<td style="text-align:center">等于</td>
</tr>
<tr>
<td style="text-align:center"><code>-ne</code></td>
<td style="text-align:center">不等于</td>
</tr>
<tr>
<td style="text-align:center"><code>-gt</code></td>
<td style="text-align:center">大于</td>
</tr>
<tr>
<td style="text-align:center"><code>-lt</code></td>
<td style="text-align:center">小于</td>
</tr>
<tr>
<td style="text-align:center"><code>-ge</code></td>
<td style="text-align:center">大于等于</td>
</tr>
<tr>
<td style="text-align:center"><code>-le</code></td>
<td style="text-align:center">小于等于</td>
</tr>
</tbody>
</table>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 判断脚本传入参数个数是否为0</span></span><br><span class="line">if [ $# -gt 0 ]; then</span><br><span class="line">  echo "Parameter Error!"</span><br><span class="line">fi</span><br></pre></td></tr></table></figure>
<h2 id="逻辑判断"><a class="markdownIt-Anchor" href="#逻辑判断"></a> 逻辑判断</h2>
<table>
<thead>
<tr>
<th style="text-align:center">命令</th>
<th style="text-align:center">含义</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>-a</code></td>
<td style="text-align:center">逻辑与</td>
</tr>
<tr>
<td style="text-align:center"><code>-o</code></td>
<td style="text-align:center">逻辑或</td>
</tr>
<tr>
<td style="text-align:center"><code>!</code></td>
<td style="text-align:center">逻辑非</td>
</tr>
</tbody>
</table>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">if [ "None" = MODEL -a "None" = IMAGE ]; then</span><br><span class="line">  echo "Parameter Error!"</span><br><span class="line">fi</span><br></pre></td></tr></table></figure>
<h2 id="字符串判断"><a class="markdownIt-Anchor" href="#字符串判断"></a> 字符串判断</h2>
<table>
<thead>
<tr>
<th style="text-align:center">命令</th>
<th style="text-align:center">含义</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>=</code></td>
<td style="text-align:center">字符串是否相等</td>
</tr>
<tr>
<td style="text-align:center"><code>!=</code></td>
<td style="text-align:center">字符串是否不相等</td>
</tr>
<tr>
<td style="text-align:center"><code>-n</code></td>
<td style="text-align:center">字符串的长度是否不等于0</td>
</tr>
<tr>
<td style="text-align:center"><code>-z</code></td>
<td style="text-align:center">字符串的长度是否等于0</td>
</tr>
</tbody>
</table>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">STRING=""</span><br><span class="line">if [ -n "$&#123;STRING&#125;" ]; then</span><br><span class="line">  echo "Everything"</span><br><span class="line">fi</span><br><span class="line"><span class="meta">#</span><span class="bash"> 注意 若<span class="variable">$&#123;STRING&#125;</span>不加双引号, 则也是认为字符串长度大于0</span></span><br></pre></td></tr></table></figure>
<h2 id="去除字符串的双引号"><a class="markdownIt-Anchor" href="#去除字符串的双引号"></a> 去除字符串的双引号</h2>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash">&#123;str&#125; | sed <span class="string">'s/\"//g'</span></span></span><br></pre></td></tr></table></figure>
<p>shell 判断文件是否存在</p>
<h2 id="文件目录判断"><a class="markdownIt-Anchor" href="#文件目录判断"></a> 文件/目录判断</h2>
<table>
<thead>
<tr>
<th style="text-align:center">命令</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>-r</code></td>
<td>文件是否存在且<strong>可读</strong></td>
</tr>
<tr>
<td style="text-align:center"><code>-w</code></td>
<td>文件是否存在且<strong>可写</strong></td>
</tr>
<tr>
<td style="text-align:center"><code>-s</code></td>
<td>文件是否存在且<strong>长度非0</strong></td>
</tr>
<tr>
<td style="text-align:center"><code>-f</code></td>
<td>文件是否存在且是普通文件</td>
</tr>
<tr>
<td style="text-align:center"><code>-d</code></td>
<td>目录是否存在</td>
</tr>
</tbody>
</table>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">if [ ! -d "/data/" ];then</span><br><span class="line">  mkdir /data</span><br><span class="line">else</span><br><span class="line">  echo "文件夹已经存在"</span><br><span class="line">fi</span><br></pre></td></tr></table></figure>
<h2 id="提取文件名和目录名"><a class="markdownIt-Anchor" href="#提取文件名和目录名"></a> 提取文件名和目录名</h2>
<h3 id="从路径中获取文件名"><a class="markdownIt-Anchor" href="#从路径中获取文件名"></a> 从路径中获取文件名</h3>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">PATH="/data/local/tmp/file.txt"</span><br><span class="line">echo $&#123;PATH##*/&#125;</span><br><span class="line"><span class="meta">#</span><span class="bash"> 结果为 file.txt</span></span><br></pre></td></tr></table></figure>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">PATH="/data/local/tmp/file.txt"</span><br><span class="line">echo $(basename $&#123;PATH&#125;)</span><br><span class="line"><span class="meta">#</span><span class="bash"> 结果为 file.txt</span></span><br><span class="line"></span><br><span class="line">echo $(basename $&#123;PATH&#125; .txt)</span><br><span class="line"><span class="meta">#</span><span class="bash"> 结果为 file</span></span><br></pre></td></tr></table></figure>
<h3 id="从路径中获取文件后缀名"><a class="markdownIt-Anchor" href="#从路径中获取文件后缀名"></a> 从路径中获取文件后缀名</h3>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">PATH="/data/local/tmp/file.tar.gz"</span><br><span class="line"><span class="meta">#</span><span class="bash"> 返回从左边算起的最后一个<span class="string">'.'</span>（不含该字符）的右边的内容</span></span><br><span class="line">echo $&#123;PATH##*.&#125;</span><br><span class="line"><span class="meta">#</span><span class="bash"> 结果为 gz</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 返回从左边算起第一个<span class="string">'.'</span>（不含该字符）的右边部分的内容</span></span><br><span class="line">echo $&#123;PATH#*.&#125;</span><br><span class="line"><span class="meta">#</span><span class="bash"> 结果为 tar.gz</span></span><br></pre></td></tr></table></figure>
<h2 id="从路径中获取文件所在目录路径"><a class="markdownIt-Anchor" href="#从路径中获取文件所在目录路径"></a> 从路径中获取文件所在目录路径</h2>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">PATH="/data/local/tmp/file.txt"</span><br><span class="line">echo $&#123;PATH%/*&#125;</span><br><span class="line"><span class="meta">#</span><span class="bash"> 结果为 /data/<span class="built_in">local</span>/tmp</span></span><br><span class="line"></span><br><span class="line">echo $&#123;PATH%%.*&#125;</span><br><span class="line"><span class="meta">#</span><span class="bash"> 结果为 /data/<span class="built_in">local</span>/tmp/file</span></span><br></pre></td></tr></table></figure>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">PATH="/data/local/tmp/file.txt"</span><br><span class="line">echo $(dirname $&#123;PATH&#125;)</span><br><span class="line"><span class="meta">#</span><span class="bash"> 结果为 /data/<span class="built_in">local</span>/tmp</span></span><br><span class="line"></span><br><span class="line">PATH="/data/local/tmp</span><br><span class="line">echo $(dirname $&#123;PATH&#125;)</span><br><span class="line"><span class="meta">#</span><span class="bash"> 结果为 /data/<span class="built_in">local</span></span></span><br></pre></td></tr></table></figure>
<h2 id="变量替换"><a class="markdownIt-Anchor" href="#变量替换"></a> 变量替换</h2>
<table>
<thead>
<tr>
<th>命令</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>$variable</code></td>
<td>保存在variable中的值</td>
</tr>
<tr>
<td><code>${variable}</code></td>
<td>保存在variable中的值</td>
</tr>
<tr>
<td><code>${variable}:-string</code></td>
<td>如果variable值为<strong>非空</strong>, 则<strong>值为variable</strong>, <strong>否则为string</strong></td>
</tr>
<tr>
<td><code>${variable}:+string</code></td>
<td>如果variable值为<strong>非空</strong>, 则<strong>值为string</strong>, <strong>否则为空</strong></td>
</tr>
<tr>
<td><code>${variable}:=string</code></td>
<td>如果variable值为<strong>非空</strong>, 则<strong>值为variable</strong>, <strong>否则为string</strong>且variable的值设为string</td>
</tr>
<tr>
<td><code>${variable}?string</code></td>
<td>如果variable值为<strong>非空</strong>, 则<strong>值为variable</strong>，<strong>否则显示string并退出</strong></td>
</tr>
</tbody>
</table>
<p>作用: 使用户能够检查变量的值并根据选项改变它的值。</p>
<h2 id="数组遍历"><a class="markdownIt-Anchor" href="#数组遍历"></a> 数组遍历</h2>
<h3 id="for循环"><a class="markdownIt-Anchor" href="#for循环"></a> for循环</h3>
<h4 id="带数组下标"><a class="markdownIt-Anchor" href="#带数组下标"></a> 带数组下标</h4>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">MODELS=("AAA" "BBB" "CCC")</span><br><span class="line">for ((i = 0; i &lt; $&#123;#MODELS[@]&#125;; i++)) </span><br><span class="line">do</span><br><span class="line">  echo $i $&#123;MODELS[i]&#125;</span><br><span class="line">done</span><br></pre></td></tr></table></figure>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">MODELS=("AAA" "BBB" "CCC")</span><br><span class="line">for i in "$&#123;!MODELS[@]&#125;"</span><br><span class="line">do   </span><br><span class="line">  printf "%s\t%s\n" "$i" "$&#123;MODELS[$i]&#125;"  </span><br><span class="line">done</span><br></pre></td></tr></table></figure>
<h4 id="不带数组下标"><a class="markdownIt-Anchor" href="#不带数组下标"></a> 不带数组下标</h4>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> @ 可用 * 替代</span></span><br><span class="line">MODELS=("AAA" "BBB" "CCC")</span><br><span class="line">for MODEL in $&#123;MODELS[@]&#125;</span><br><span class="line">do</span><br><span class="line">  echo $MODEL</span><br><span class="line">done</span><br></pre></td></tr></table></figure>
<h3 id="while循环"><a class="markdownIt-Anchor" href="#while循环"></a> While循环</h3>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">MODELS=("AAA" "BBB" "CCC")</span><br><span class="line">i=0  </span><br><span class="line">while [ $i -lt $&#123;#MODELS[@]&#125; ]  </span><br><span class="line"><span class="meta">#</span><span class="bash">当变量（下标）小于数组长度时进入循环体</span></span><br><span class="line">do  </span><br><span class="line">  echo $&#123;MODELS[$i]&#125;  </span><br><span class="line"><span class="meta">  #</span><span class="bash">按下标打印数组元素</span></span><br><span class="line">  let i++  </span><br><span class="line">done</span><br></pre></td></tr></table></figure>
<h3 id="解析脚本传参"><a class="markdownIt-Anchor" href="#解析脚本传参"></a> 解析脚本传参</h3>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">!/bin/sh</span></span><br><span class="line"></span><br><span class="line">set -e</span><br><span class="line"></span><br><span class="line">current_dir=$(cd `dirname $0`; pwd)</span><br><span class="line"></span><br><span class="line">USAGE_LINE="Usage: $0 [OPTION] [SELECT]\n                     \</span><br><span class="line">                --input-model\t\t测试模型路径\n                 \</span><br><span class="line">                --input-picture\t测试模型的输入数据文件\n       \</span><br><span class="line">                --help\t\t\t帮助\n                               \</span><br><span class="line">e.g: $0 --input-model=./test.pb --input-picture=./test.jpg    \</span><br><span class="line">"</span><br><span class="line"></span><br><span class="line">for opt in "$&#123;@&#125;"; do</span><br><span class="line">  case $opt in</span><br><span class="line">    --input-model=*)</span><br><span class="line">      export input_model="$(echo "$opt" | cut -d '=' -f 2-)"</span><br><span class="line">      ;;</span><br><span class="line">    --input-picture=*)</span><br><span class="line">      export input_picture="$(echo "$opt" | cut -d '=' -f 2-)"</span><br><span class="line">      ;;</span><br><span class="line">    --help)</span><br><span class="line">      echo $&#123;USAGE_LINE&#125;</span><br><span class="line">      exit</span><br><span class="line">      ;;</span><br><span class="line">    *)</span><br><span class="line">      echo $&#123;USAGE_LINE&#125;</span><br><span class="line">      exit</span><br><span class="line">      ;;</span><br><span class="line">  esac</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line">echo $&#123;input_model&#125; $&#123;input_picture&#125;</span><br></pre></td></tr></table></figure>
<h3 id="格式化输出"><a class="markdownIt-Anchor" href="#格式化输出"></a> 格式化输出</h3>
<ul>
<li>
<p>输出类型</p>
<ul>
<li><code>%ns</code>：输出<strong>字符串</strong>。n 是数字，指代输出几个字符。</li>
<li><code>%ni</code>：输出<strong>整数</strong>。n 是数字，指代输出几个数字。</li>
<li><code>%m.nf</code>: 输出<strong>浮点数</strong>。m 和 n 是数字，指代输出的整数位数和小数位数。如 %8.2f 代表共输出 8 位数，其中 2 位是小数，6 位是整数。</li>
</ul>
</li>
<li>
<p>输出格式</p>
<ul>
<li><code>\a</code>： 输出警告声音；</li>
<li><code>\b</code>：输出退格键，也就是 Backspaced 键；</li>
<li><code>\f</code>：清除屏幕；</li>
<li><code>\n</code>：换行；</li>
<li><code>\r</code>：回车，也就是 Enter 键；</li>
<li><code>\t</code>：水平输出退格键，也就是 Tab 键；</li>
<li><code>\v</code>：垂直输出退格键，也就是 Tab 键；</li>
</ul>
</li>
<li>
<p>示例</p>
<ul>
<li>
<p>有这样一个文本 <strong>tmp.txt</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">ID Name PHP Linux MySQL Average</span><br><span class="line">1 Liming 82 95 86 87.66</span><br><span class="line">2 Sc 74 96 87 85.66</span><br><span class="line">3 Gao 99 83 93 91.66</span><br></pre></td></tr></table></figure>
</li>
<li>
<p>格式化输出</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">printf '%s\t %s\t %s\t %s\t %s\t %s\t\n' $(cat tmp.txt)</span><br><span class="line"><span class="meta">#</span><span class="bash"> 输出结果</span></span><br><span class="line">ID       Name    PHP     Linux   MySQL   Average</span><br><span class="line">1        Liming  82      95      86      87.66</span><br><span class="line">2        Sc      74      96      87      85.66</span><br><span class="line">3        Gao     99      83      93      91.66</span><br><span class="line"></span><br><span class="line">printf '%i\t %s\t %i\t %i\t %i\t %.2f\t\n' $(cat tmp.txt | grep -v Name)</span><br><span class="line"><span class="meta">#</span><span class="bash"> 输出结果</span></span><br><span class="line">1        Liming  82      95      86      87.66</span><br><span class="line">2        Sc      74      96      87      85.66</span><br><span class="line">3        Gao     99      83      93      91.66</span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>

      
    </div>
    <div class="article-footer">
      <!-- <blockquote class="mt-2x">
  <ul class="post-copyright list-unstyled">
    
    <li class="post-copyright-link hidden-xs">
      <strong>本文链接：</strong>
      <a href="http://zhulao.gitee.io/blog/2019/02/26/Shell脚本常用命令/" title="Shell脚本常用命令" target="_blank" rel="external">http://zhulao.gitee.io/blog/2019/02/26/Shell脚本常用命令/</a>
    </li>
    
    <li class="post-copyright-license">
      <strong>版权声明： </strong> 本博客所有文章除特别声明外，均采用 <a href="http://creativecommons.org/licenses/by/4.0/deed.zh" target="_blank" rel="external">CC BY 4.0 CN协议</a> 许可协议。转载请注明出处！
    </li>
  </ul>
</blockquote>


<div class="panel panel-default panel-badger">
  <div class="panel-body">
    <figure class="media">
      <div class="media-left">
        <a href="https://github.com/wzx3" target="_blank" class="img-burn thumb-sm visible-lg">
          <img src="/blog/images/avatar.jpeg" class="img-rounded w-full" alt="">
        </a>
      </div>
      <div class="media-body">
        <h3 class="media-heading"><a href="https://github.com/wzx3" target="_blank"><span class="text-dark">王竹兴</span><small class="ml-1x"></small></a></h3>
        <div></div>
      </div>
    </figure>
  </div>
</div>
 -->

    </div>
  </article>
  
    
  <section id="comments">
  	
  </section>


  
</div>

  <nav class="bar bar-footer clearfix" data-stick-bottom>
  <div class="bar-inner">
  
  <ul class="pager pull-left">
    
    <li class="prev">
      <a href="/blog/2019/02/28/hello-world/" title="Hello World"><i class="icon icon-angle-left" aria-hidden="true"></i><span>&nbsp;&nbsp;上一篇</span></a>
    </li>
    
    
    <li class="next">
      <a href="/blog/2019/02/21/Shell脚本处理JSON数据工具-jq/" title="Shell脚本处理JSON数据工具: jq"><span>下一篇&nbsp;&nbsp;</span><i class="icon icon-angle-right" aria-hidden="true"></i></a>
    </li>
    
    
    <li class="toggle-toc">
      <a class="toggle-btn collapsed" data-toggle="collapse" href="#collapseToc" aria-expanded="false" title="文章目录" role="button">
        <span>[&nbsp;</span><span>文章目录</span>
        <i class="text-collapsed icon icon-anchor"></i>
        <i class="text-in icon icon-close"></i>
        <span>]</span>
      </a>
    </li>
    
  </ul>
  
  
  
  <div class="bar-right">
    
    <div class="share-component" data-sites="weibo,qq,wechat,facebook,twitter" data-mobile-sites="weibo,qq,qzone"></div>
    
  </div>
  </div>
</nav>
  


</main>

  <footer class="footer" itemscope itemtype="http://schema.org/WPFooter">
	
	
    <ul class="social-links">
    	
        <li><a href="https://github.com/wzx3" target="_blank" title="Github" data-toggle="tooltip" data-placement="top"><i class="icon icon-github"></i></a></li>
        
        <li><a href="/blog/atom.xml" target="_blank" title="Rss" data-toggle="tooltip" data-placement="top"><i class="icon icon-rss"></i></a></li>
        
    </ul>

    <div class="copyright">
    	
        <div class="publishby">
        	Theme by <a href="https://github.com/cofess" target="_blank"> cofess </a>base on <a href="https://github.com/cofess/hexo-theme-pure" target="_blank">pure</a>.
        </div>
    </div>
</footer>
  <script src="//cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<script>
window.jQuery || document.write('<script src="js/jquery.min.js"><\/script>')
</script>
<script src="/blog/js/plugin.min.js"></script>
<script src="/blog/js/application.js"></script>

    <script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: '文章',
            PAGES: '页面',
            CATEGORIES: '分类',
            TAGS: '标签',
            UNTITLED: '(未命名)',
        },
        ROOT_URL: '/blog/',
        CONTENT_URL: '/blog/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>
<script src="/blog/js/insight.js"></script>





   
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>





   






</body>
</html>